package com.test.qykj.qyelectromobile.utils;

import android.view.View;
import android.widget.ProgressBar;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.EncryptUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * Created by Erebus on 2016/8/24.
 */
public class HttpUtil {
    private static final int REQUEST_TIMEOUT = 15000;
    private final String TAG = getClass().getSimpleName();
    private static final String VERSION = AppUtils.getAppVersionName();
    //    测试 url
//    public static final String URL = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP ?
//            "https://app.ridingking.com:9001" : "http://app.ridingking.com:8021") + "/ElectricBicyclePro/";
    public static final String URL = "https://app.99bicycle.com/ElectricBicyclePro/";//正式URL
    //   public static final String MOVIE_URL = "https://app.ridingking.com:9001/ElectricBicycleActivity/";
    public static final String MOVIE_URL = "https://active.99bicycle.com/ElectricBicycleActivity/";//正式

    public static RequestParams getRequestParams(String action) {
        return new RequestParams(URL + action);
    }

    private HttpUtil() {
    }

    private static String[] sign() {
        String str1 = getRandomString(10);
        String str = VERSION + str1 + "zjqy20141229";
        return new String[]{str1, EncryptUtils.encryptMD5ToString(str)};
    }

    private static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static void httpPost(RequestParams params,
                                final int action,
                                final HttpRequestCallback callBack,
                                final ProgressBar progressBar
    ) {
        if (progressBar != null) progressBar.setVisibility(View.VISIBLE);
        String[] sign = sign();
        params.setConnectTimeout(REQUEST_TIMEOUT);
        params.addBodyParameter("version", VERSION);
        params.addBodyParameter("randomNum", sign[0]);
        params.addBodyParameter("sign", sign[1]);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                callBack.onSuccess(action, result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                callBack.onFailure(action, ex, ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                if (progressBar != null) progressBar.setVisibility(View.GONE);
            }
        });
    }

    public static void baiduHttpPost(RequestParams params, final HttpRequestCallback callBack, final int action) {
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                callBack.onSuccess(action, result);


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                callBack.onFailure(action, ex, ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public interface HttpRequestCallback {
        void onSuccess(int action, String result);

        void onFailure(int action, Throwable e, String s);
    }
}
